{% extends 'layout/basic.html' %}
{% load static %}
{% block css %}
    <link rel="stylesheet" href="{% static 'css/account.css' %}">
    <style>
        .error-msg {
            color: #ff0000;
            position: absolute;
            font-size: 13px;
        }
    </style>
{% endblock %}
{% block content %}
    <div class="account">
        <div class="title">短信验证码登录</div>
        <form id="loginSmsForm" method="POST" novalidate>
            {% csrf_token %}
            {% for field in form %}
                {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        <div class="row">
                            <div class="col-xs-7">
                                {{ field }}
                                <span class="error-msg">{{ field.errors.0 }}</span>
                            </div>
                            <div class="col-xs-5">
                                <input id="btnSms" type="button" class="btn btn-default" value="点击获取验证码">
                            </div>
                        </div>
                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        {{ field }}
                        <span class="error-msg">{{ field.errors.0 }}</span>
                    </div>
                {% endif %}
            {% endfor %}

            <div>
                <div style="float: right;">
                    <a href="{% url 'login' %}">账号密码登录?</a>
                </div>
            </div>

            <div class="row">
                <div class="col-xs-3">
                    <input id="btnSubmit" type="button" class="btn btn-primary" value="登  录"/>
                </div>
            </div>
        </form>
    </div>

{% endblock %}
{% block js %}
    <script>
        $(function () {
            BindBtnSubmitClick();
            BindBtnSmsClick();
        })

        function BindBtnSubmitClick() {
            $("#btnSubmit").click(function () {
                $(".error-msg").empty();
                $.ajax({
                    url: "{% url 'login_sms' %}",
                    type: "POST",
                    data: $("#loginSmsForm").serialize(),
                    dataType: "JSON",
                    success: function (res) {
                        console.log(res);

                        if (res['status']) {
                            location.href = "{% url 'home' %}";
                        } else {
                            $("#id_code").val();
                            $.each(res['errors'], function (key, value) {
                                $("#id_" + key).next().text(value);
                            })
                        }

                    }
                })
            })
        }

        function BindBtnSmsClick() {
            $("#btnSms").click(function () {
                $(".error-msg").empty();
                //获取用户输入的手机号
                var mobilePhone = $("#id_mobile_phone").val();
                $.ajax({
                    url: "{% url 'send_sms' %}",
                    type: 'GET',
                    data: {mobile_phone: mobilePhone, tpl: 'login'},
                    dataType: "JSON",
                    success: function (res) {
                        console.log(res);
                        if(res['status']){
                            $("#id_code").val(res['sms']);
                            $("#id_mobile_phone").next().text('');
                            //设置按钮不可操作
                            $("#btnSms").prop("disabled",true);
                            //定时器
                            var time = 60;
                            var obj = setInterval(function () {
                                time = time -1;
                                $("#btnSms").val('('+time+')重新获取验证码');
                                if(time < 1){
                                    //清除定时器
                                    clearInterval(obj);
                                    $("#btnSms").prop("disabled",false);
                                    $("#btnSms").val('点击获取验证码');
                                }
                            },1000)
                        }else {
                            $("#id_code").val('');
                            $.each(res['errors'], function (key, value) {
                                $("#id_" + key).next().text(value);
                            })
                        }
                    }
                })
            })
        }
    </script>
{% endblock %}